package com.puboot.common.util;

import com.puboot.module.admin.model.BizArticle;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

/***
 * 分发文章到其他平台
 * @author chengrongkai
 */
@Slf4j
@UtilityClass
public class DistributionArticleUtil {

    private final static String BAIJIAHAO_PATH = "http://baijiahao.baidu.com/builderinner/open/resource/article/publish";

    public static boolean distribution(ArrayList<BizArticle> list, String[] distributionTypes){
        if (null == list || list.size() == 0) {
            return false;
        }
        boolean result = false;
        for (String distributionType : distributionTypes) {
            switch (distributionType){
                case "baijiaohao": result = distributionBaijiahao(list);break;
                case "toutiaohao":result = distributionToutiaohao(list);break;
                case "cnblogs":result = distributionCnblogs(list);break;
                case "csdn":result = distributionCsdn(list);break;
                case "zhihu":result = distributionZhihu(list);break;
                default:
            }
        }
        return result;
    }

    /**
     * 分发到百家号
     * @param list
     * @return
     */
    private static boolean distributionBaijiahao(ArrayList<BizArticle> list){
        return false;
    }

    /***
     * 分发到头条号
     * @param list
     * @return
     */
    private static boolean distributionToutiaohao(ArrayList<BizArticle> list){
        return false;
    }

    /**
     * 分发到博客园
     * @param list
     * @return
     */
    private static boolean distributionCnblogs(ArrayList<BizArticle> list){
        return false;
    }

    /**
     * 分发到csdn
     * @param list
     * @return
     */
    private static boolean distributionCsdn(ArrayList<BizArticle> list){
        return false;
    }

    /**
     * 分发到知乎
     * @param list
     * @return
     */
    private static boolean distributionZhihu(ArrayList<BizArticle> list){
        return false;
    }


    /*推送百度*/
    public static String postBaidu(String postUrl, String[] parameters) {
        if (null == postUrl || null == parameters || parameters.length == 0) {
            return null;
        }
        StringBuilder result = new StringBuilder();

        //建立URL之间的连接
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) new URL(postUrl).openConnection();
            //设置通用的请求属性
            conn.setRequestProperty("Host", "data.zz.baidu.com");
            conn.setRequestProperty("User-Agent", "curl/7.12.1");
            conn.setRequestProperty("Content-Length", "83");
            conn.setRequestProperty("Content-Type", "text/plain");
            conn.setRequestMethod("POST");
            //发送POST请求必须设置如下两行
            conn.setDoInput(true);
            conn.setDoOutput(true);
        } catch (IOException e) {
            log.error("【推送百度】建立URL之间的连接失败:{}", e.getMessage(), e);
        }
        try (PrintWriter out = new PrintWriter(conn.getOutputStream())) {

            //发送请求参数
            StringBuilder param = new StringBuilder();
            for (String s : parameters) {
                param.append(s).append('\n');
            }
            out.print(param.toString().trim());
            //进行输出流的缓冲
            out.flush();
            //通过BufferedReader输入流来读取Url的响应
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        } catch (IOException e) {
            log.error("推送百度出现异常:{}", e.getMessage(), e);
        }
        return result.toString();
    }
}
